﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace EasyClass {
	public class Task {
		public string taskName;
		public List<Problem> problems;
		public int status;  //0: pending, 1: running, 2: finished

		public Task(string name) {
			taskName = name;
			problems = new List<Problem>();
			status = 0;
		}

		public Panel GetPanel() {
			MyConsole.WriteLine("Task " + taskName + "'s GetPanel is called, # of problems = " + problems.Count.ToString());
			FlowLayoutPanel panel = new FlowLayoutPanel();
			panel.AutoSize = true;
			panel.FlowDirection = FlowDirection.TopDown;
			int cnt = 1;
			foreach (Problem p in problems) {
				panel.Controls.Add(p.GetPanel(cnt++));
			}
			return panel;
		}

		public void EditButton_Click(object sender, EventArgs e) {
			if (this.status != 0) {
				MessageBox.Show("Task is not pending, operation refused");
				return;
			}
			ProblemButton button = (ProblemButton)sender;
			EditProblemsForm ef = new EditProblemsForm(button.problem, this);
			ef.ShowDialog();
			Controller.GetController.UpdateTaskPanel(this);
		}

		public void DeleteButton_Click(object sender, EventArgs e) {
			if (this.status != 0) {
				MessageBox.Show("Task is not pending, operation refused");
				return;
			}
			ProblemButton button = (ProblemButton)sender;
			problems.Remove(button.problem);
			Controller.GetController.UpdateTaskPanel(this);
		}

		public Dictionary<string, string> ToDict() {
			Dictionary<string, string> dict = new Dictionary<string, string>();
			dict["TaskName"] = taskName;
			dict["Status"] = status.ToString();
			dict["NProblems"] = problems.Count.ToString();
			int cnt = 0;
			foreach (Problem problem in problems)
				dict["Problem" + cnt++.ToString()] = problem.ToJSON();
			return dict;
		}
		
	}
}
